home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / JPNL Libraries / TCPClient.java < prev    next >
Text File  |  1996-05-23  |  2KB  |  71 lines

  1. package au.com.peter.libraries;
  2.  
  3. import java.io.*;
  4. import java.net.Socket;
  5.  
  6. public class TCPClient {
  7.  
  8.     public Socket connection = null;
  9.     public OutputStream output;
  10.     public InputStream input;
  11.     
  12.     public void OpenConnection( String hostport ) throws IOException, ParameterException {
  13.         int p = hostport.indexOf( ':' );
  14.         if ( p < 0 ) {
  15.             throw new ParameterException( "Missing ':' in " + hostport );
  16.         }
  17.         String host = hostport.substring( 0, p );
  18.         int port = Integer.parseInt( host.substring( p + 1 ) );
  19.         DoOpenConnection( new Socket( host, port ) );
  20.     }
  21.     
  22.     public void OpenConnection( String host, int port ) throws IOException {
  23.         DoOpenConnection( new Socket( host, port ) );
  24.     }
  25.     
  26.     public void DoOpenConnection( Socket conn ) throws IOException {
  27.         if ( ConnectionIsOpen() ) {
  28.             CloseConnection();
  29.         }
  30.         connection = conn;
  31.         output = ConnectOutputStream( connection.getOutputStream() );
  32.         input = ConnectInputStream( connection.getInputStream() );
  33.     }
  34.     
  35.     public OutputStream ConnectOutputStream( OutputStream output ) {
  36.         return new BufferedOutputStream( output );
  37.     }
  38.  
  39.     public InputStream ConnectInputStream( InputStream input ) {
  40.         return input;
  41.     }
  42.  
  43.     public void CloseConnection() {
  44.         if ( ConnectionIsOpen() ) {
  45.             try {
  46.                 connection.close();
  47.             } catch ( Exception e );
  48.             connection = null;
  49.             input = null;
  50.             output = null;
  51.         }
  52.     }
  53.     
  54.     public void CheckOpen() throws IOException {
  55.         if ( !ConnectionIsOpen() ) {
  56.             throw new TCPClientException( "connection is not open" );
  57.         }
  58.     }
  59.  
  60.     public boolean ConnectionIsOpen() {
  61.         return connection != null;
  62.     }
  63.  
  64. }
  65.  
  66. class TCPClientException extends IOException {
  67.     public TCPClientException( String s ) {
  68.         super( s );
  69.     }
  70. }
  71.